<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Resource as ResourceModel;
use app\admin\model\ResourceCheck as ResourceCheckModel;
use app\admin\model\ResourceRecommend as ResourceRecommendModel;
use think\facade\Log;

class ResourceService
{
    /**
     * 获取本月社群（折线图）
     * @return mixed
     */
    public static function getResourceMonthLine()
    {
        $startEndTime = getStartEndTime();
        $resourceMonth = ResourceModel::getResourceMonth1($startEndTime['startTime'], $startEndTime['endTime']);
        return inArr($resourceMonth, $startEndTime['dateArr']);
    }

    /**
     * 社群统计
     * @param array $where
     * @return int
     */
    public static function countResource($where = [])
    {
        return ResourceModel::countResource($where);
    }

    /**
     * 昨日新增社群
     * @return int
     */
    public static function yesTodayResource()
    {
        return ResourceModel::yesTodayResource();
    }

    /**
     * 本周新增社群
     * @return int
     */
    public static function countResourceWeek()
    {
        return ResourceModel::countResourceWeek();
    }

    /**
     * 本月新增社群
     * @return int
     */
    public static function countResourceMonth()
    {
        return ResourceModel::countResourceMonth();
    }

    /**
     * 社群地区分布
     * @return mixed
     */
    public static function getResourceArea()
    {
        $data = ResourceModel::getResourceArea();
        $datas = getAreaData($data);
        return ['count' => count($data), 'datas' => $datas];
    }

    /**
     * 今日新增
     * @return int
     */
    public static function todayResource()
    {
        return ResourceModel::todayResource();
    }

    /**
     * 需求分类
     * @return array
     */
    public static function resourceCate()
    {
        $resourceCate = ResourceModel::resourceCate();
        $colorArr = [];
        $datas = [];
        foreach ($resourceCate as $k => $v) {
            $colorArr[] = $v['color'];
            $datas[] = json_encode([
                'name' => $v['name'],
                'value' => $v['nums'],
            ], JSON_UNESCAPED_UNICODE);
        }
        $line_data['colors'] = join(',', $colorArr);
        $line_data['datas'] = join(';', $datas);
        return ['resourceCate' => $resourceCate, 'line_data' => $line_data];
    }

    /**
     * 社群字段数据
     * @param $where
     * @return array
     */
    public static function resourceFields($where)
    {
        return ResourceModel::resourceFields($where);
    }

    /**
     * 社群列表
     * @param $where
     * @param $where1
     * @param $page
     * @param $pagesize
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getResourceList($where, $where1, $page, $pagesize)
    {
        $list = ResourceModel::getResourceList($where, $where1, $page, $pagesize);

        $list = self::assembleList($list);

        return $list;
    }

    /**
     * 统计社群列表
     * @param $where
     * @param $where1
     * @return int
     */
    public static function countResourceList($where, $where1)
    {
        return ResourceModel::countResourceList($where, $where1);
    }

    /**
     * 推荐列表
     * @param $where
     * @param $where1
     * @param $page
     * @param $pagesize
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecommendList($where, $where1, $page, $pagesize)
    {
        $list = ResourceRecommendModel::getRecommendList($where, $where1, $page, $pagesize);

        $list = self::assembleList($list, 1);

        return $list;
    }

    /**
     * 统计推荐列表
     * @param $where
     * @return int
     */
    public static function countRecommendList($where)
    {
        return ResourceRecommendModel::countRecommendList($where);
    }

    /**
     * 审核列表
     * @param $where
     * @param $where1
     * @param $page
     * @param $pagesize
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCheckList($where, $page, $pagesize)
    {
        $list = ResourceCheckModel::getResourceList($where, $page, $pagesize);

        $list = self::assembleList($list);

        return $list;
    }

    /**
     * 统计审核列表
     * @param $where
     * @return int
     */
    public static function countCheckList($where)
    {
        return ResourceCheckModel::countCheckList($where);
    }

    /**
     * 组装列表数据
     * @param $list
     * @param int $type
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected static function assembleList($list, $type = 0)
    {
        foreach ($list as $k => $v) {
            $list[$k]['ids'] = $v['id'] . '<br><span class="color3">' . $v['number'] . '</span>';
            // LOGO
            $logo = '<div class="mar-lefts"><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['logo'] . '" \' + \'alt=""></div>';
            // 名称数据
            $list[$k]['name_data'] = '<div class="d-fix1">' . $logo . '<p class="datas">' . $v['name'] . '/<span class="color3">' . $v['cate_name'] . '</span></p></div>';

            // 二维码
            $qrcode = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['qrcode'] . '" \' + \'alt=""></div>';
            if (strrpos($v['city_name'], '市')) {
                $v['city_name'] = substr_replace($v['city_name'], '', strrpos($v['city_name'], '市'));
            }
            // 企业数据
            $list[$k]['company_data'] = '<div class="d-fix1">' . $qrcode . '<div><p class="datas">' . $v['company'] . '/<span class="color3">' . $v['city_name'] . '</span></p>';


            // 用户数据
            $users = UsersService::findUsers(['id' => $v['uid']]);
            Log::write('用户id数据-' . $v['uid'], 'error');
//            $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $users['headimg'] . '" \' + \'alt=""></div>';
            $list[$k]['users_data'] = '<p style="text-align: center" class="datas spaces">' . ($users ? emojiDecode($users['nickname']) : '未知') . '</p>';
            if (!$type) {
                // 创建数据
                $accessCount = ResourceClickShareService::countResourceClickShare(['rid' => $v['id'], 'type' => 1]);

                $list[$k]['create_time'] = date('Y-m-d H:i', (is_string($v['create_time']) ? strtotime($v['create_time']) : $v['create_time']));
                $list[$k]['create_data'] = $list[$k]['create_time'] . '<br><span class="color3">' . $accessCount . '</span>';

                // 是否已推荐
                $is_recommend = ResourceRecommendService::findResourceRecommend(['rid' => $v['id'], 'type' => 2]);
                $list[$k]['is_recommend'] = $is_recommend ? 1 : 0;

                // 推荐列表
            } else {
                $add_time = date('Y-m-d H:i', $v['add_time']);
                $end_time = date('Y-m-d H:i', $v['end_time']);
                $show_time = $v['show_time'] > 0 ? date('Y-m-d H:i', $v['show_time']) : '-';
                $list[$k]['create_data'] = '<p class="color1">' . $add_time . '</p><p class="color2">' . $end_time . '</p><p>' . $show_time . '</p>';
            }
        }

        return $list;
    }

    /**
     * 单社群数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findResource($where)
    {
        return ResourceModel::findResource($where);
    }

    /**
     * 更新社群数据
     * @param $data
     * @param $where
     * @return ResourceModel
     */
    public static function updateResource($data, $where)
    {
        return ResourceModel::updateResource($data, $where);
    }

    /**
     * 创建社群数据
     * @param $data
     * @return ResourceModel
     */
    public static function createResource($data)
    {
        return ResourceModel::createResource($data);
    }
}